//
//  TogetherBaiduMapViewController.m
//  Together_iOS
//
//  Created by QIU Shuang on 3/1/14.
//  Copyright (c) 2014 QIU Shuang. All rights reserved.
//

#import "TogetherBaiduMapViewController.h"
#import "TogetherDateArrangerCell.h"
#import "Stop.h"

@interface TogetherBaiduMapViewController ()

@property (nonatomic, assign) CGRect contentFrame;
@property (nonatomic, strong) BMKMapView *baiduMapView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) BOOL setGeo;

@end

@implementation TogetherBaiduMapViewController

@synthesize contentFrame  = _contentFrame;
@synthesize baiduMapView  = _baiduMapView;

@synthesize targetGeoPoint = _targetGeoPoint;
@synthesize setGeo = _setGeo;
@synthesize stops = _stops;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.setGeo = NO;
    }
    return self;
}

- (void) loadView {
    [super loadView];
    
    // initialize segmented view control
    NSArray *tags = [NSArray arrayWithObjects:@"地图", @"安排", nil];
    self.view.backgroundColor = [[UIColor alloc] initWithRed:0.965 green:0.957 blue:0.918 alpha:1];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:tags];
    segmentedControl = [[UISegmentedControl alloc] initWithItems:tags];
    segmentedControl.frame = CGRectMake(60, 70, 200, 30);
    segmentedControl.selectedSegmentIndex = 0;
    [segmentedControl addTarget:self action:@selector(segmentSelected:) forControlEvents:UIControlEventValueChanged];
    [segmentedControl setTintColor:[[UIColor alloc] initWithRed:255./255. green:114./255. blue:19./255. alpha:1.0]];
    [self.view addSubview:segmentedControl];
    
    // initialize Baidu map view
    self.baiduMapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 105, 320, 463)];
    self.baiduMapView.zoomEnabled = YES;
    self.baiduMapView.scrollEnabled = YES;
    
    // initialize table view
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.frame = CGRectMake(10, 105, 300, 450);
    [self.tableView setBackgroundColor:[[UIColor alloc] initWithRed:0.965 green:0.957 blue:0.918 alpha:1]];
    
    [self segmentSelected:segmentedControl];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.tableView.separatorColor = [UIColor clearColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(beginToEdit)];
    
    // enable user location
    self.baiduMapView.showsUserLocation = NO;
    self.baiduMapView.userTrackingMode = BMKUserTrackingModeNone;
    self.baiduMapView.showsUserLocation = YES;
}

- (void) beginToEdit {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - SegmentedControl protocol
- (void) segmentSelected:(id) sender {
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    NSLog(@"%d", [segmentedControl selectedSegmentIndex]);
    switch ([segmentedControl selectedSegmentIndex]) {
        case 0:
        {
            [self.tableView removeFromSuperview];
            [self.view addSubview:self.baiduMapView];
            BMKMapStatus *mapStatus = [[BMKMapStatus alloc] init];
            if ([self.stops count] && !self.setGeo) {
                Stop *s = (Stop *)[self.stops objectAtIndex:0];
                self.targetGeoPoint = CLLocationCoordinate2DMake([s.latitude doubleValue], [s.longtitude doubleValue]);
                self.setGeo = YES;
            }
            
            if (self.setGeo) {
                mapStatus.targetGeoPt = self.targetGeoPoint;
            }
            
            [self.baiduMapView setMapStatus:mapStatus withAnimation:YES];

            for (Stop *stop in self.stops) {
                BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc] init];
                CLLocationCoordinate2D coor;
                coor.latitude = [stop.latitude doubleValue];
                coor.longitude = [stop.longtitude doubleValue];
                pointAnnotation.coordinate = coor;
                pointAnnotation.title = stop.name;
                [self.baiduMapView addAnnotation:pointAnnotation];
            }
        }
            break;
        case 1:
            [self.baiduMapView removeFromSuperview];
            [self.view addSubview:self.tableView];
            
            [self.tableView reloadData];
            break;
        default:
            break;
    }
}

- (void)setTargetGeoPoint:(CLLocationCoordinate2D)targetGeoPoint {
    _targetGeoPoint = targetGeoPoint;
    self.setGeo = YES;
}

#pragma mark - UITableViewDelegate protocol
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.stops count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"dateReadonlyCell";
    TogetherDateArrangerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[TogetherDateArrangerCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    cell.event = [self.stops objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.shouldDrag = NO;
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

@end
